package com.sd365.permission.centre.api.controller;

import com.sd365.common.core.common.exception.BusinessException;
import com.sd365.common.core.common.exception.code.BusinessErrorCode;
import com.sd365.common.log.api.annotation.ApiLog;
import com.sd365.common.util.BeanUtil;
import com.sd365.permission.centre.api.AuditApi;
import com.sd365.permission.centre.entity.Audit;
import com.sd365.permission.centre.pojo.dto.AuditDTO;
import com.sd365.permission.centre.pojo.query.AuditQuery;
import com.sd365.permission.centre.pojo.vo.AuditVO;
import com.sd365.permission.centre.service.AuditService;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class AuditController implements AuditApi {
    @Resource
    AuditService auditService;
    /**
     * 通用查询，带分页信息
     *
     * @param auditQuery
     * @return
     */
    @Override
    @ApiLog
    public List<AuditVO> commonQuery(AuditQuery auditQuery) {
        List<Audit> audits = auditService.commonQuery(auditQuery);
        List<AuditVO> auditVOS;
        try {
            auditVOS = BeanUtil.copyList(audits, AuditVO.class);
        } catch (Exception e) {
            throw new BusinessException(BusinessErrorCode.SYSTEM_BEAN_COPY_EXCEPTION, e);
        }
        return auditVOS;
    }

    @Override
    @ApiLog
    public AuditVO selectById(Long id) {
        AuditDTO auditDTO = auditService.queryById(id);
        if (auditDTO != null) {
            return BeanUtil.copy(auditDTO, AuditVO.class);
        } else {
            return null;
        }
    }

    @Override
    public List<AuditVO> selectByUseSimplePwd(Byte isusp) {
        List<AuditDTO> auditDTOS = auditService.selectByUseSimplePwd(isusp);
        if (auditDTOS != null) {
            return BeanUtil.copyList(auditDTOS, AuditVO.class);
        } else {
            return null;
        }
    }

    @Override
    public List<AuditVO> selectByNoUpdatePwd(Byte isnup) {
        List<AuditDTO> auditDTOS = auditService.selectByNoUpdatePwd(isnup);
        if (auditDTOS != null) {
            return BeanUtil.copyList(auditDTOS, AuditVO.class);
        } else {
            return null;
        }
    }

    @Override
    public List<AuditVO> selectByLongNoLogin(Byte islnl) {
        List<AuditDTO> auditDTOS = auditService.selectByLongNoLogin(islnl);
        if (auditDTOS != null) {
            return BeanUtil.copyList(auditDTOS, AuditVO.class);
        } else {
            return null;
        }
    }
}
